//
//  LSCollectionDataBase.m
//  NineGroupProject
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "LSCollectionDataBase.h"
#import <FMDB.h>
#import "SubjectModel.h"
@interface LSCollectionDataBase ()

@property (nonatomic, retain)FMDatabase *db;

@end

@implementation LSCollectionDataBase

+ (instancetype)shareDataBase{
    static LSCollectionDataBase *dbSub = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dbSub = [[LSCollectionDataBase alloc] init];
    });
    return dbSub;
}

- (void)openDB{
    if (self.db != nil ) {
        NSLog(@"数据库已经打开");
        return;
    }
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [doc stringByAppendingPathComponent:@"Collect.sqlite"];
    NSLog(@"fileName = %@", fileName);
    
    self.db = [FMDatabase databaseWithPath:fileName];
    
    if ([self.db open]) {
        NSLog(@"数据库打开成功");
    }else {
        NSLog(@"数据库打开失败");
    }
}
/**
 *  @property (nonatomic, assign)NSInteger experience_id;
 @property (nonatomic, strong)NSString *experience_title;
 @property (nonatomic, strong)NSString *relate_img;
 */
- (void)createTable{
    BOOL result = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS subCollect (id INTEGER PRIMARY KEY AUTOINCREMENT, experience_id INTEGER, experience_title TEXT, relate_img TEXT);"];
    if (result) {
        NSLog(@"创收藏表成功");
    }else{
        NSLog(@"创收藏表失败");
    }
}
- (void)insertSub:(NSInteger)experience_id Title:(NSString *)experience_title Img:(NSString *)relate_img{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO subCollect(experience_id, experience_title, relate_img) VALUES('%ld', '%@', '%@')", experience_id, experience_title,relate_img];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}
- (NSMutableArray *)select:(NSInteger)idNum{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM subCollect WHERE experience_id = '%ld'",idNum];
    FMResultSet *resultSet = [self.db executeQuery:sql];
    while ([resultSet next]) {
        NSString *nameC = [resultSet objectForColumnName:@"experience_title"];
        NSString *img = [resultSet objectForColumnName:@"relate_img"];
        NSInteger idNum = [resultSet intForColumn:@"experience_id"];
        SubjTVModel *subM = [[SubjTVModel alloc] init];
        subM.experience_id = idNum;
        subM.experience_title = nameC;
        subM.relate_img = img;
        [array addObject:subM];
    }
    return array;
}


- (NSMutableArray *)select{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM subCollect"];
    FMResultSet *resultSet = [self.db executeQuery:sql];
    while ([resultSet next]) {
        NSString *nameC = [resultSet objectForColumnName:@"experience_title"];
        NSString *img = [resultSet objectForColumnName:@"relate_img"];
        NSInteger idNum = [resultSet intForColumn:@"experience_id"];
        SubjTVModel *subM = [[SubjTVModel alloc] init];
        subM.experience_id = idNum;
        subM.experience_title = nameC;
        subM.relate_img = img;
        [array addObject:subM];
    }
    return array;
}
- (void)delete:(NSString *)name{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM subCollect WHERE experience_title = '%@'", name];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    
}

@end
